package com.fengcheng.config;

import com.fengcheng.pojo.User;
import com.fengcheng.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("执行了认证");
        //  连接数据库
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        User user = userService.queryByName(userToken.getUsername());
        if (user.getName()== null) {
            return null;
        }
        // 密码认证shiro自己去做
        return new SimpleAuthenticationInfo("",user.getPassword(), "" );
    }
}
